package com.ruoyi.cms.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.cms.domain.ShortVideo;
import com.ruoyi.cms.service.IShortVideoService;

/**
 * 短视频用户端接口
 *
 * @author ruoyi
 * @date 2025-10-24
 */
@RestController
@RequestMapping("/short")
public class AppShortController extends BaseController
{
    @Autowired
    private IShortVideoService shortVideoService;

    /**
     * 获取短视频列表（前台）
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(ShortVideo shortVideo)
    {
        // 只显示正常状态的短视频
        shortVideo.setStatus("0");
        startPage();
        List<ShortVideo> list = shortVideoService.selectShortVideoList(shortVideo);
        return getDataTable(list);
    }

    /**
     * 获取短视频详情（前台）
     */
    @Anonymous
    @GetMapping("/{videoId}")
    public AjaxResult getDetail(@PathVariable("videoId") Long videoId)
    {
        ShortVideo video = shortVideoService.selectShortVideoById(videoId);
        if (video == null) {
            return AjaxResult.error("短视频不存在");
        }

        // 增加播放量
        shortVideoService.increaseViewCount(videoId);

        return AjaxResult.success(video);
    }

    /**
     * 获取用户短视频统计
     */
    @Anonymous
    @GetMapping("/user/{userId}/stats")
    public AjaxResult getUserStats(@PathVariable("userId") Long userId)
    {
        Map<String, Object> stats = new HashMap<>();

        // 获取用户短视频数量
        ShortVideo query = new ShortVideo();
        query.setUserId(userId);
        query.setStatus("0");
        List<ShortVideo> videos = shortVideoService.selectShortVideoList(query);

        Long totalLikes = 0L;
        for (ShortVideo video : videos) {
            if (video.getLikes() != null) {
                totalLikes += video.getLikes();
            }
        }

        stats.put("shortsCount", videos.size());
        stats.put("likesCount", totalLikes);
        stats.put("fansCount", 0); // 粉丝数暂时返回0，后续实现

        return AjaxResult.success(stats);
    }

    /**
     * 获取用户短视频列表
     */
    @Anonymous
    @GetMapping("/user/{userId}/list")
    public TableDataInfo getUserVideoList(@PathVariable("userId") Long userId)
    {
        ShortVideo shortVideo = new ShortVideo();
        shortVideo.setUserId(userId);
        shortVideo.setStatus("0");
        startPage();
        List<ShortVideo> list = shortVideoService.selectShortVideoList(shortVideo);
        return getDataTable(list);
    }

    /**
     * 获取推荐短视频
     */
    @Anonymous
    @GetMapping("/recommend")
    public AjaxResult getRecommend()
    {
        ShortVideo shortVideo = new ShortVideo();
        shortVideo.setStatus("0");
        // 按播放量降序排序
        List<ShortVideo> list = shortVideoService.selectShortVideoList(shortVideo);
        return AjaxResult.success(list);
    }

    /**
     * 获取热门短视频
     */
    @Anonymous
    @GetMapping("/hot")
    public AjaxResult getHot(@RequestParam(defaultValue = "10") Integer limit)
    {
        ShortVideo shortVideo = new ShortVideo();
        shortVideo.setStatus("0");
        List<ShortVideo> list = shortVideoService.selectHotVideos(limit);
        return AjaxResult.success(list);
    }

    /**
     * 搜索短视频
     */
    @Anonymous
    @GetMapping("/search")
    public TableDataInfo search(@RequestParam String keyword)
    {
        ShortVideo shortVideo = new ShortVideo();
        // TODO: 搜索功能需要重构，新设计中使用description字段替代title
        // shortVideo.setDescription(keyword);
        shortVideo.setStatus("0");
        startPage();
        List<ShortVideo> list = shortVideoService.selectShortVideoList(shortVideo);
        return getDataTable(list);
    }
}
